using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using DataAccessLayer;

namespace WebUI.Custom_Cotrols
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TaskControl runat=server></{0}:TaskControl>")]
    public class TaskControl : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int TaskID
        {
            get
            {
                int i = (int)ViewState["TaskID"];
                return (i);
            }

            set
            {
                ViewState["TaskID"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Description
        {
            get
            {
                String s = (String)ViewState["Description"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Description"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int LocationID
        {
            get
            {
                int i = (int)ViewState["LocationID"];
                return (i);
            }

            set
            {
                ViewState["LocationID"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime StartTime
        {
            get
            {
                DateTime dt = (DateTime)ViewState["StartTime"];
                return (dt);
            }

            set
            {
                ViewState["StartTime"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime EndTime
        {
            get
            {
                DateTime dt = (DateTime)ViewState["EndTime"];
                return (dt);
            }

            set
            {
                ViewState["EndTime"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Addressed
        {
            get
            {
                bool b = (bool)ViewState["Addressed"];
                return (b);
            }

            set
            {
                ViewState["Addressed"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Status
        {
            get
            {
                String s = (String)ViewState["Status"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Status"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime Created
        {
            get
            {
                DateTime dt = (DateTime)ViewState["Created"];
                return (dt);
            }

            set
            {
                ViewState["Created"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime Modified
        {
            get
            {
                DateTime dt = (DateTime)ViewState["Modified"];
                return (dt);
            }

            set
            {
                ViewState["Modified"] = value;
            }
        }

        private DataRow _task;
        private DataTable _purchaseOrders;
        private Table _notes;
        private DataTable _locations;
        private DataRow _location;

        private TextBox txtDescription;
        private DropDownList ddlLocation;
        private Button btnSave;

        public TaskControl()
        {

        }

        public TaskControl(DataRow task,int customerID , int userID)
        {
            _task = task;
            TaskID = (int)task.ItemArray[0];
            Description = (string)task.ItemArray[1];
            LocationID = (int)task.ItemArray[2];
            if (task.ItemArray[3] != DBNull.Value)
            {
                StartTime = (DateTime)task.ItemArray[3];
            }
            if (task.ItemArray[4] != DBNull.Value)
            {
                EndTime = (DateTime)task.ItemArray[4];
            }
            Addressed = (bool)task.ItemArray[5];
            Status = (string)task.ItemArray[6];
            Created = (DateTime)task.ItemArray[7];
            Modified = (DateTime)task.ItemArray[8];
            DAL.GetLocationsByCustomerID(userID, customerID ,out _locations);
            DAL.GetPurchaseOrdersByTaskID(userID, TaskID,out _purchaseOrders);

            ddlLocation = new DropDownList();
            ddlLocation.DataSource = _locations;

            ddlLocation.DataValueField = "LocationID";
            ddlLocation.DataTextField = "LocationName";
            ddlLocation.DataBind();
            foreach (DataRow dr in _locations.Rows)
            {
                //ddlLocation.Items.Add(new ListItem(dr.ItemArray[1].ToString(), dr.ItemArray[0].ToString()));
                if ((int)dr.ItemArray[0] == (int)_task.ItemArray[2])
                {
                    _location = dr;
                    break;
                }
            }
            int i = 0;
            foreach (ListItem li in ddlLocation.Items)
            {
                if (int.Parse(li.Value) == (int)_location.ItemArray[0])
                {
                    ddlLocation.SelectedIndex = i;
                    break;
                }
                i++;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            btnSave.Text = "Yeah!! I fired an event!!!!";
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            Panel pnlBorder = new Panel();
            pnlBorder.BorderStyle = BorderStyle.Solid;
            pnlBorder.RenderBeginTag(output);
            if (this._task != null)
            {
                output.Write(
            "\n<table rules=\"all\" >\n" +
            "   <tr style=\"background-color:Gray;\">\n" +
            "       <td>Description: </td>\n" +
            "       <td>Location:</td>\n" +
            "       <td>Start Time:</td>\n" +
            "       <td>End Time</td>\n" +
            "       <td>Addressed</td>\n" +
            "       <td>Status</td>\n" +
            "       <td>Created</td>\n" +
            "       <td>Modified</td>\n" +
            "   </tr>\n" +
            "   <tr>\n" +
            "       <td>\n" +
            "           ");
                txtDescription = new TextBox();
                txtDescription.Text = Description;
                txtDescription.RenderBeginTag(output);
                //txtDescription.RenderControl(output);
                txtDescription.RenderEndTag(output);

                output.Write(
            "       </td>\n" +
            "       <td>\n" +
            "           ");// + _location.ItemArray[1].ToString() + "<br />\n" +

                ddlLocation.RenderControl(output);
            output.Write(    
            //"           " + _location.ItemArray[2].ToString() + "<br />\n" +
            //"           " + _location.ItemArray[3].ToString() + "<br />\n" +
            //"           " + _location.ItemArray[4].ToString() + "," + _location.ItemArray[5].ToString() + " " + _location.ItemArray[6].ToString() + "<br />\n" +
            
            "       </td>\n" +
            "       <td>\n" + 
            "           " + StartTime.ToLongTimeString() +
            "       </td>\n" +
            "       <td>\n" +
            "           " + EndTime.ToLongTimeString() +
            "       </td>\n" +
            "       <td>\n" +
            "            <input id=\"Checkbox1\" type=\"checkbox\" disabled=\"disabled\" " +
            ((Addressed) ? "checked=\"checked\" " : "") +
            "/>\n" +
            "       </td>\n" +
            "       <td>" + Status + "</td>\n" +
            "       <td>" + Created.ToShortDateString() + "</td>\n" +
            "       <td>" + Modified.ToShortDateString() + "</td>\n" +
            "   </tr>\n" +
            "</table>\n" +
            "<p>Purchse Orders for this Task</p>\n");
                GridView gvPurchaseOrders = new GridView();
                BoundField poID = new BoundField();
                poID.DataField = "PurchaseOrderID";
                poID.HeaderText = "poID";
                poID.Visible = false;
                gvPurchaseOrders.Columns.Add(poID);
                BoundField logonName = new BoundField();
                logonName.DataField = "LogOnName";
                logonName.HeaderText = "Added By";
                gvPurchaseOrders.Columns.Add(logonName);
                BoundField description = new BoundField();
                description.DataField = "Description";
                description.HeaderText = "Description";
                gvPurchaseOrders.Columns.Add(description);
                BoundField Manufacturer = new BoundField();
                Manufacturer.DataField = "Manufacturer";
                Manufacturer.HeaderText = "Manufacturer";
                gvPurchaseOrders.Columns.Add(Manufacturer);
                BoundField ModelNumber = new BoundField();
                ModelNumber.HeaderText = "ModelNumber";
                ModelNumber.DataField = "ModelNumber";
                gvPurchaseOrders.Columns.Add(ModelNumber);
                BoundField Quantity = new BoundField();
                Quantity.DataField = "Quantity";
                Quantity.HeaderText = "Quantity";
                gvPurchaseOrders.Columns.Add(Quantity);
                BoundField Price = new BoundField();
                Price.HeaderText = "Price";
                Price.DataField = "Price";
                gvPurchaseOrders.Columns.Add(Price);


                gvPurchaseOrders.AutoGenerateColumns = false;
                gvPurchaseOrders.DataSource = _purchaseOrders;
                gvPurchaseOrders.DataBind();


                gvPurchaseOrders.RenderBeginTag(output);
                gvPurchaseOrders.RenderControl(output);
                gvPurchaseOrders.RenderEndTag(output);
                output.Write("<br />");
            }
            pnlBorder.RenderEndTag(output);
        }
    }
}
